package com.fas.web.controller.system;

import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.entity.SysUser;
import com.fas.common.core.domain.model.RegisterBody;
import com.fas.common.utils.StringUtils;
import com.fas.framework.web.service.SysRegisterService;
import com.fas.system.domain.FasIpqc;
import com.fas.system.domain.vo.MaintenanceUnitVo;
import com.fas.system.mapper.FasIpqcMapper;
import com.fas.system.mapper.SysDeptMapper;
import com.fas.system.mapper.SysUserMapper;
import com.fas.system.service.ISysConfigService;
import com.fas.system.service.ISysDeptService;
import com.fas.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysDeptService deptService;

    @Resource
    private SysDeptMapper deptMapper;

    @Resource
    private ISysUserService userService;
    @Resource
    private SysUserMapper userMapper;

    @Resource
    private FasIpqcMapper fasIpqcMapper;


    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 小程序工程师注册
     * @param user
     * @return
     */
    @PostMapping("/miniappEngineerRegister")
    public AjaxResult miniappEngineerRegister(@RequestBody RegisterBody user)
    {
        if (StringUtils.isBlank(user.getPhonenumber())) {
            return AjaxResult.error("手机号不能为空");
        }
        SysUser sysUser = userMapper.checkPhoneUnique(user.getPhonenumber());
        if (sysUser != null) {
            return AjaxResult.error("手机号已经被注册");
        }
        if (StringUtils.isBlank(user.getCredentials())) {
            return AjaxResult.error("工程师凭证不能为空");
        }
        SysDept query = new SysDept();
        query.setDeptType("0");
        List<SysDept> sysDepts = deptMapper.selectDeptList(query);
        SysDept dept = sysDepts.get(0);
        user.setDeptId(dept.getDeptId());
        user.setStatus("2");
        user.setUsername(user.getPhonenumber());
        String msg = registerService.miniappEngineerRegister(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 小程序维保单位和巡检员用户注册
     * @param registerBody
     * @return
     */
    @ApiOperation("小程序维保单位和巡检员用户注册")
    @PostMapping("/miniappMaintainRegister")
    public AjaxResult miniappMaintainRegister(@RequestBody RegisterBody registerBody)
    {
        String phonenumber = registerBody.getPhonenumber();
        if (StringUtils.isBlank(phonenumber)) {
            return AjaxResult.error("手机号不能为空");
        }
        SysUser user = userMapper.checkPhoneUnique(phonenumber);
        if (user != null) {
            return AjaxResult.error("手机号已经被注册");
        }
        String deptName = registerBody.getDeptName();
        if (StringUtils.isBlank(deptName)) {
            return AjaxResult.error("错误，维保单位信息为空！");
        }
        if (registerBody.getUserType().equals("02")){ //维保单位才判断渠道商
            if (registerBody.getParentId() == null) {
                return AjaxResult.error("错误，渠道商为空！");
            }
            SysDept dept1 = new SysDept();
            dept1.setParentId(registerBody.getParentId());
            //因为目前只有三级，所有直接根据渠道商查维保单位
            List<MaintenanceUnitVo> sysDepts1 = deptService.selectMaintenanceUnitVoList(dept1);
            if (CollectionUtils.isEmpty(sysDepts1)) {
                return AjaxResult.error("错误，渠道商下面暂无维保单位，请联系管理员");
            }
            List<String> deptNames = sysDepts1.stream().map(MaintenanceUnitVo::getDeptName).collect(Collectors.toList());
            if (!deptNames.contains(registerBody.getDeptName())) {
                return AjaxResult.error("错误，渠道商下面暂无该维保单位，请联系管理员");
            }
            registerBody.setStatus("2");
        }

//        registerBody.setNickName(registerBody.getDeptName());
        registerBody.setUsername(phonenumber);
        SysDept query = new SysDept();
        query.setDeptName(registerBody.getDeptName());
        List<MaintenanceUnitVo> depts = deptService.selectMaintenanceUnitVoList(query);
        if (CollectionUtils.isEmpty(depts)) {
            return AjaxResult.error("错误，未能查询到相关维保单位！");
        }
        MaintenanceUnitVo vo = depts.get(0);
        Long deptId = vo.getDeptId();
        registerBody.setDeptId(deptId);

        // 巡检员
        if (registerBody.getUserType().equals("03")){
            // 判断用户的手机号是否早维保单位所创建的列表中
            FasIpqc fasIpqc = fasIpqcMapper.selectByContactNumber(phonenumber,deptId);
            if (Objects.isNull(fasIpqc)) {
                return AjaxResult.error("维保单位不存在该巡检员");
            }
            registerBody.setNickName(fasIpqc.getiName());
            registerBody.setStatus("0");
        }
        String msg = registerService.miniappMaintainRegister(registerBody);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
}
